local pofu = fk.CreateSkill {
  name = "inker_pofu",
  tags = { Skill.Compulsory }
}

local pofuCheckFunc = function(room)
  local cardNames = {}
  room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
    for _, move in ipairs(e.data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          local c = Fk:getCardById(info.cardId)
          if c.type == Card.TypeBasic and not table.contains(cardNames, c.trueName) and room:getCardArea(c) == Card.DiscardPile then
            table.insert(cardNames, c.trueName)
          end
        end
      end
    end
  end, Player.HistoryTurn)
  return cardNames
end

local getPofued = function(player)
  local room = player.room
  room:setPlayerMark(player, "@@inker_pofu-turn", 1)
  room:setPlayerMark(player, "_inker_pofu_mark-turn", pofuCheckFunc(room))
  for _, to in ipairs(room.alive_players) do
    to:filterHandcards()
  end
end

pofu:addAcquireEffect(function(self, player, is_start)
  if not is_start then
    local room = player.room
    if room:getCurrent() == player or room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.from == player and move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            if table.contains({ Player.Hand, Player.Equip }, info.fromArea) then
              return true
            end
          end
        end
      end
    end, Player.HistoryTurn)[1] then
      room:notifySkillInvoked(player, pofu.name)
      player:broadcastSkillInvoke(pofu.name)
      getPofued(player)
    end
  end
end)
pofu:addLoseEffect(function(self, player)
  local room = player.room
  room:setPlayerMark(player, "@@inker_pofu-turn", 0)
  room:setPlayerMark(player, "_inker_pofu_mark-turn", 0)
  for _, to in ipairs(room.alive_players) do
    to:filterHandcards()
  end
end)

pofu:addEffect(fk.TurnStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(pofu.name) and player:getMark("@@inker_pofu-turn") == 0
  end,
  on_use = function(self, event, target, player, data)
    getPofued(player)
  end
})
pofu:addEffect(fk.AfterCardsMove, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(pofu.name) or player:getMark("@@inker_pofu-turn") ~= 0 then return false end
    for _, move in ipairs(data) do
      if move.from == player and move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard then
        for _, info in ipairs(move.moveInfo) do
          if table.contains({ Player.Hand, Player.Equip }, info.fromArea) then
            return true
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    getPofued(player)
  end,

  can_refresh = function(self, event, target, player, data)
    if player:getMark("@@inker_pofu-turn") == 0 then return false end
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        return true
      elseif move.from == nil then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.DiscardPile then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    getPofued(player)
  end,
})
pofu:addEffect("filter", {
  mute = true,
  card_filter = function(self, to_select, player)
    local guanping = table.find(Fk:currentRoom().alive_players, function(p) return p:getMark("@@inker_pofu-turn") ~= 0 end)
    if not guanping then return false end
    return to_select.type == Card.TypeBasic and table.contains(player:getCardIds("h"), to_select.id) and not table.contains(guanping:getTableMark("_inker_pofu_mark-turn"), to_select.trueName)
  end,
  view_as = function(self, player, to_select)
    return Fk:cloneCard("slash", to_select.suit, to_select.number)
  end,
})

Fk:loadTranslationTable{
  ["inker_pofu"] = "破釜",
  [":inker_pofu"] = "锁定技，回合内或你弃置牌后，本回合未进入弃牌堆的基本牌的同名牌视为【杀】。",

  ["@@inker_pofu-turn"] = "破釜",
}

return pofu